function Transpaydept(){
	this.form=this.InitForm();
	this.orgsuytable=this.InitOrgSuyTable();
	this.tab=this.InitTab();
	this.table=null;
	
	$("#year").val($("#hidyear").val());
	$("#month").val($("#hidmonth").val());
	this.form.render('select');
	
	$("#bottom_btns").hide();
	
	
}
Transpaydept.prototype.InitTab=function(){
	var element;
	layui.use('element', function(){
	   element = layui.element;
	   
	   element.on('tab(tabcontent)', function(data){
		    if(data.index==1){
		    	$("#bottom_btns").show();
				if($("#hidstatus").val()!=''){
					var status=Number($("#hidstatus").val());
					if(status>0){
						$("#btnbmtj").attr("disabled","disabled");
						$("#btnbmtj").html("该部门已确认");
					}
				}
		    }else{
		    	$("#hidorgid").val(0);
		   	    $("#hidorgsummaryid").val(0);
		   	    $("#hidstatus").val('');
				$("#bottom_btns").hide();
				$("#btnbmtj").removeAttr("disabled");
				
				$("#realname").val("");
		    }
	   });
	   
	});
	return element;
}

Transpaydept.prototype.OpTab=function(tabindex, obj){
	this.tab.tabChange('tabcontent', tabindex);
}

Transpaydept.prototype.InitForm=function(){
	var form;
	layui.use('form', function(){
       form = layui.form; //只有执行了这一步，部分表单元素才会自动修饰成功
       form.on('select(year)', function (data) { 
    	   $("#hidyear").val(data.value);
    	   form.render('select');   
    	   
    	   vTransPayDept.searchtableorgsuy();
       });
       form.on('select(month)', function (data) { 
    	   $("#hidmonth").val(data.value);
    	   form.render('select');   
    	   vTransPayDept.searchtableorgsuy();
       });
       
	   form.render();
	});  
	return form;
}

Transpaydept.prototype.searchlist = function () {
    this.grid.setOptions({
        newPage: 1, parms: {
            Method: "",
            year: $("#hidyear").val(),
			month:$("#hidmonth").val()
        }
    });
    this.grid.loadData();
}

Transpaydept.prototype.InitOrgSuyTable = function() {
	var table;
	var _this=this;
	layui.use('table', function() {
	    table = layui.table;
		table.render({
			elem : '#orgsuylist'
			,height : $(window).height() - 140
			,url : 'get_org_tran_dept_list' // 数据接口
			,page : true // 开启分页
			,limit : 30
			,id: 'orgsuylist'
			,method:"post"
			,cols : [ [
			           {type:'numbers'}
			           , {field : 'orgname',title : '单位名称',width : 250,align : 'center'}
			           , {field : 'year',title : '年',width : 100,align : 'center'}
			           , {field : 'quarter',title : '季度',width : 100,align : 'center'}
			           , {field : 'month',title : '月份',width : 100,align : 'center'}
			           , {field : 'orgsummaryid',title : '部门人数',width : 100,align : 'center'}
			           , {field : 'status',title : '状态',width : 120,align : 'center'}
			           , {title : '操作',width : 100,align : 'left',toolbar : '#operateorgsuy'}
		            ] ]
		    ,request:{
				type:'post'
			}
		    ,page:false
			,where:{
				year: $("#hidyear").val(),
				month:$("#hidmonth").val()
				}
			,even: true //开启隔行背景
		    ,done: function(res, page, count){
		    	var dataarr=res.data;
		    	$("[data-field='status']").children().each(function(index){ 
		    		if(index>0){
		    			var text=$(this).text();
		    			switch(text){
		    			case null:
		    				$(this).text("未填报");
		    				break;
		    			case '':
		    				$(this).text("未填报");
		    				break;
		    			case '0':
		    				$(this).text("填报");
		    				break;
		    			case '1':
		    				$(this).text("确认");
		    				break;
		    			case '2':
		    				$(this).text("归档");
		    				break;
		    			case '3':
		    				$(this).text("驳回");
		    				break;
						case '4':
							$(this).text("审核通过");
                            break;
		    			}
		    		}
               })  
               
               
               $("[data-field='quarter']").children().each(function(index){ 
		    		if(index>0){
		    			var text=$(this).text();
		    			switch(text){
		    			case null:
		    			case '':
		    				var month=dataarr[index-1].month;
		    				if(month==1||month==2||month==3){
		    					$(this).text("第一季度");
		    				}
		    				if(month==4||month==5||month==6){
		    					$(this).text("第二季度");
		    				}
		    				if(month==7||month==8||month==9){
		    					$(this).text("第三季度");
		    				}
		    				if(month==10||month==11||month==12){
		    					$(this).text("第四季度");
		    				}
		    				break;
		    			case '1':
		    				$(this).text("第一季度");
		    				break;
		    			case '2':
		    				$(this).text("第二季度");
		    				break;
		    			case '3':
		    				$(this).text("第三季度");
		    				break;
		    			case '4':
		    				$(this).text("第四季度");
		    				break;
		    			}
		    		}
              })
                $("[data-field='orgsummaryid']").children().each(function(index){
                    if(index>0) {
                        var thisdata = dataarr[index - 1];
                        var text = $(this).text();
                        if(text == null || text ==''){
                        	console.log(1);
                            var percount = thisdata.usercount;
                            $(this).text(percount);
                        }else{
                        	console.log(2);
                            var realusercount = thisdata.realusercount;
                            $(this).text(realusercount);
                        }
                    }
                })

		    }
		});

		
		 //监听工具条
	    table.on('tool(orgsuylist)', function(obj){
	      var data = obj.data;
	      if(obj.event === 'detail'){
	    	  $("#hidorgid").val(data.orgid);
	    	  $("#hidorgsummaryid").val(data.orgsummaryid);
	    	  $("#hidstatus").val(data.status);
	    	  _this.OpTab(2,data);
	    	  
	    	  $("#table_title").html(data.orgname+" "+data.year+" 年  "+data.month+" 月考勤表");
	    	  
	    	  _this.table=_this.InitTableByBt(data.orgid,data.orgsummaryid);
	      } else if(obj.event === 'tijiao'){
	    	  vTransPayDept.updatestatus(data.orgsummaryid,1);
	      } else if(obj.event === 'shengcheng'){
	    	  vTransPayDept.createatttb(data.orgid,data.year,data.month);
	      }
	    });

	    
	});
	
	return table;
}

Transpaydept.prototype.getattdetail=function(orgid, orgsummaryid, status, orgname, year, month){
	 $("#hidorgid").val(orgid);
	 $("#hidorgsummaryid").val(orgsummaryid);
	 $("#hidstatus").val(status);
	 this.OpTab(2);
	  
	 $("#table_title").html(orgname+" "+year+" 年  "+month+" 月交通补助表");
	  
	 this.table=this.InitTableByBt(orgid,orgsummaryid);
}

Transpaydept.prototype.querydetail=function(){
	if($("#hidorgid").val()==""||$("#hidorgid").val()=="0"){
		top.mylayui.msg('请先点机构列表后面详情按钮，再点击查询', {icon: 0});
		return;
	}
	
	
	this.InitTableByBt($("#hidorgid").val(), $("#hidorgsummaryid").val());
}

Transpaydept.prototype.InitTableByBt=function (orgid, orgsummaryid){
	var table;
	var _this=this;
	  if(_this.table!=null){
		  _this.table.reload('attbmhzlist', { 
			  page: {
		        curr: 1 //重新从第 1 页开始
		      }
		    ,where: {  
		    	//year: $("#detailyear").val(),
		    	//mouth:$("#detailmonth").val(),
		    	year: '',
				mouth:'',
		    	orgid:orgid,
		    	orgsummaryid:orgsummaryid,
		    	realname:$("#realname").val()
		    }  
		}); 
	  }
	  else{
		layui.use('table', function() {
	    table = layui.table;
	    table.init('attbmhzlist', {
			height : $(window).height() - 210
			,url : 'get_tran_dept_list' // 数据接口
			,page : true // 开启分页
			,limit : 40
			,id: 'attbmhzlist'
			,limits:[20,40,80,100]
			,method:"post"
				,request:{
					type:'post'
				}
				,where:{
					//year: $("#detailyear").val(),
					//mouth:$("#detailmonth").val(),
					year: '',
					mouth:'',
					orgid:orgid,
					orgsummaryid:orgsummaryid,
					realname:$("#realname").val()
					}
			    ,done: function(res, page, count){
			    	
			    	var dataarr=res.data;
			    	$("[data-field='status']").children().each(function(index){ 
			    		if(index>0){
			    			var text=$(this).text();
			    			switch(text){
			    			case null:
			    				$(this).text("未填报");
			    				break;
			    			case '':
			    				$(this).text("未填报");
			    				break;
			    			case '0':
			    				$(this).text("填报");
			    				break;
			    			case '1':
			    				$(this).text("确认");
			    				break;
			    			case '2':
			    				$(this).text("归档");
			    				break;
			    			case '3':
			    				$(this).text("驳回");
			    				break;
			    			}
			    		}
	               })  
	               
	               
	               $("[data-field='sfqq']").children().each(function(index){ 
			    		if(index>0){
			    			var text=$(this).text();
			    			if(text=="0"){
			    				$(this).text("否");
			    			}
			    			else{
			    				$(this).text("是");
			    			}
			    		}
	               })
	               
//	               $("[data-field='sfjb']").children().each(function(index){ 
//			    		if(index>0){
//			    			var text=$(this).text();
//			    			if(text=="0"){
//			    				$(this).text("否");
//			    			}
//			    			else{
//			    				$(this).text("是");
//			    			}
//			    		}
//	               })
	               
	               
			    }
				
				
		  });
	    
	    
	    //监听工具条
	    table.on('tool(attbmhzlist)', function(obj){
	      var data = obj.data;
	      if(obj.event === 'detail'){
	    	  vTransPayDept.openadddetail(obj.data.attsummaryid,obj.data.orgsummaryid);
	      } else if(obj.event === 'del'){
	    	  
	        top.mylayui.confirm('确定要删除吗？',{icon: 0}, function(index,dd){
	        	 top.mylayui.close(index);
	        	 vUser.deleteentity(data.userid);
	        });
	      } else if(obj.event === 'edit'){
	        //layer.alert('编辑行：<br>'+ JSON.stringify(data))
	    	  //_this.OpTab(2,data);
	    	  //vUser.openadd(obj.data.userid);
	      }
	      else if(obj.event === 'btntz'){
	    	  vTransPayDept.openadd(obj.data.attsummaryid,obj.data.orgsummaryid,obj.data.userid);
		  }
	    });
	    
	    
	})
	  }
	return table;
}

Transpaydept.prototype.openadd=function(attsummaryid, orgsummaryid, userid){
//	top.mylayui.open({
//         type: 2,
//         title: '调整考勤',
//         shadeClose: true,
//         shade: 0.5,
//         area: ['1070px', '500px'],
//         content: 'attbmhz/att_bm_hz_edit?attsummaryid='+attsummaryid+"&orgsummaryid="+orgsummaryid
//       }); 
	
	top.mylayui.open({
        type: 2,
        title: '调整考勤',
        shadeClose: true,
        shade: 0.5,
        area: ['800px', '450px'],
        content: 'attbmhz/att_bm_hz_edit_nodetail?attsummaryid='+attsummaryid+"&orgsummaryid="+orgsummaryid+"&userid="+userid
      }); 

}

Transpaydept.prototype.openadddetail=function(attsummaryid, orgsummaryid){
	top.mylayui.open({
         type: 2,
         title: '考勤详情',
         shadeClose: true,
         shade: 0.5,
         area: ['1000px', '500px'],
         content: 'attbmhz/att_bm_hz_show?attsummaryid='+attsummaryid+"&orgsummaryid="+orgsummaryid
       }); 
	var iframeele=top.$(".layui-layer-content").find('iframe');
	iframeele.css({"height":$(iframeele).height()-10});
}

Transpaydept.prototype.addresult=function(result, msg){
	if(result>0){
		top.mylayui.msg('操作成功', {icon: 1});
		top.mylayui.closeAll('iframe');
		vTransPayDept.searchtable();
	}
	else{
		if(msg){
			top.mylayui.msg(msg, {icon: 0});
		}else{
			top.mylayui.msg('操作失败', {icon: 0});
		}
	}
}

Transpaydept.prototype.searchtable=function(){
	this.table.reload('attbmhzlist', { 
		  page: {
	        curr: 1 //重新从第 1 页开始
	      }
	    ,where: {  
	    	year: $("#year").val(),
	    	mouth:$("#mouth").val()
	    }  
	}); 
}


Transpaydept.prototype.exportexcel=function(){
	if($("#hidorgid").val()==""||$("#hidorgid").val()=="0"){
		top.mylayui.msg('请先点机构列表后面详情按钮，再点击查询', {icon: 0});
		return;
	}
	top.ShowZheZhao();
	$.ajax({
		type:"POST",
		url:"export_excel",
		data:{
			year: '',
			mouth:'',
			orgid:$("#hidorgid").val(),
			orgsummaryid:$("#hidorgsummaryid").val()
			},
		beforeSend:function(XHR){
			
		},
		success:function(data){
			data=eval('('+data+')');
			if(data.result>0){
				//top.mylayui.msg(data.msg, {icon: 1});
				//alert(data.msg);
				window.open(data.msg,'_self');
			}
			else{
				top.mylayui.msg(data.msg, {icon: 0});
			}
			top.ShowZheZhao();
			
		},
		complete:function(XHR){
			top.mylayui.closeAll('loading');
		},
		error:function(){
			
		}
	});
}



Transpaydept.prototype.searchtableorgsuy=function(){
	this.orgsuytable.reload('orgsuylist', { 
		  page: {
	        curr: 1 //重新从第 1 页开始
	      }
	   ,page:false
	    ,where: {  
	    	year: $("#hidyear").val(),
	    	month:$("#hidmonth").val()
	    }  
	}); 
}

Transpaydept.prototype.createatttb=function(orgid, year, month){
	
	top.ShowZheZhao();
	$.ajax({
		type:"POST",
		//url:"create_atttb",
		url:"create_tran_pay",
		data:{orgid:orgid,year: year,month:month},
		beforeSend:function(XHR){
			
		},
		success:function(data){
			data=eval('('+data+')');
			if(data.result>0){
				top.mylayui.msg(data.msg, {icon: 1});
				//vTransPayDept.searchtableorgsuy();
				vTransPayDept.searchtableorgsuy();
			}
			else{
				top.mylayui.msg(data.msg, {icon: 0});
			}
			top.ShowZheZhao();
		},
		complete:function(XHR){
			top.mylayui.closeAll('loading');
		},
		error:function(){
			
		}
	});
}

Transpaydept.prototype.updatestatus=function(orgsummaryid, status){
	 top.mylayui.confirm('点击确认后，交通补助会提交到部门进行审批',{icon: 0}, function(index,dd){
    	 top.mylayui.close(index);
	$.ajax({
		type:"POST",
		url:"update_tran_pay_status",
		data:{orgsummaryid:orgsummaryid,status:status},
		beforeSend:function(XHR){
			
		},
		success:function(data){
			data=eval('('+data+')');
			if(data.result>0){
				top.mylayui.msg(data.msg, {icon: 1});
				//vTransPayDept.searchtableorgsuy();
				vTransPayDept.searchtableorgsuy();
			}
			else{
				top.mylayui.msg(data.msg, {icon: 0});
			}
		},
		complete:function(XHR){
			
		},
		error:function(){
			
		}
	});
	 })
}

Transpaydept.prototype.btntijiao=function(){
    var orgsummaryid=$("#hidorgsummaryid").val();
	vTransPayDept.updatestatus(orgsummaryid,1);
}

var vTransPayDept=null;
$(function(){
	vTransPayDept=new Transpaydept();
})